// axios二次封装
import axios from 'axios'


import store from '@/store'

// 添加一个进度条
import nprogress from 'nprogress'
import 'nprogress/nprogress.css'



// axios.create方法执行会返回axios
let requests = axios.create({
    // 设置基础lujing
    baseURL:'/api',
    // 设置超时时间
    timeout:5000
})

// 设置请求拦截器
requests.interceptors.request.use((config)=>{
    // 进度条开始
    nprogress.start();

    
    if (store.state.shopcart.USER_ID) {
        config.headers.userTempId = store.state.shopcart.USER_ID;
    }

    if (store.state.user.token) {
        config.headers.token = store.state.user.token;
    }

    return config;

})

// 设置响应拦截器
requests.interceptors.response.use((res)=>{
    // 进度条结束
    nprogress.done();
    return res.data;

},(err)=>{
    alert(err.message)

    // 终止
    return new Promise();

})


// 暴露二次封装的axios
export default requests;